class Solution {
public:
    int powToSum(int x)
    {
        int sum=0;
        while(x)
        {
            int t = x%10;
            sum+=t*t;
            x/=10;
        }
        return sum;
    }
    bool isHappy(int n) {

        int slow = n;
        int fast = powToSum(n);
        while(slow!=fast)
        {
            slow=powToSum(slow);
            fast = powToSum(powToSum(fast));
        }
        return slow == 1;
    }
};